s3-librpc: Register NCALRPC pipes.
[nivanova/samba-autobuild/.git] / source3 / rpc_server / rpc_ep_setup.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *
4  *  SMBD RPC service callbacks
5  *
6  *  Copyright (c) 2011      Andreas Schneider <asn@samba.org>
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 3 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include "includes.h"
23
24 #include "../librpc/gen_ndr/srv_epmapper.h"
25 #include "../librpc/gen_ndr/srv_srvsvc.h"
26 #include "../librpc/gen_ndr/srv_winreg.h"
27 #include "../librpc/gen_ndr/srv_dfs.h"
28 #include "../librpc/gen_ndr/srv_dssetup.h"
29 #include "../librpc/gen_ndr/srv_echo.h"
30 #include "../librpc/gen_ndr/srv_eventlog.h"
31 #include "../librpc/gen_ndr/srv_initshutdown.h"
32 #include "../librpc/gen_ndr/srv_lsa.h"
33 #include "../librpc/gen_ndr/srv_netlogon.h"
34 #include "../librpc/gen_ndr/srv_ntsvcs.h"
35 #include "../librpc/gen_ndr/srv_samr.h"
36 #include "../librpc/gen_ndr/srv_spoolss.h"
37 #include "../librpc/gen_ndr/srv_svcctl.h"
38 #include "../librpc/gen_ndr/srv_wkssvc.h"
39
40 #include "printing/nt_printing_migrate.h"
41 #include "rpc_server/eventlog/srv_eventlog_reg.h"
42 #include "rpc_server/svcctl/srv_svcctl_reg.h"
43
44 #include "librpc/rpc/dcerpc_ep.h"
45
46 #include "rpc_server/rpc_ep_setup.h"
47 #include "rpc_server/rpc_server.h"
48
49 struct dcesrv_ep_context {
50         struct tevent_context *ev_ctx;
51         struct messaging_context *msg_ctx;
52 };
53
54 static uint16_t _open_sockets(struct tevent_context *ev_ctx,
55                               struct messaging_context *msg_ctx,
56                               struct ndr_syntax_id syntax_id,
57                               uint16_t port)
58 {
59         uint32_t num_ifs = iface_count();
60         uint32_t i;
61         uint16_t p = 0;
62
63         if (lp_interfaces() && lp_bind_interfaces_only()) {
64                 /*
65                  * We have been given an interfaces line, and been told to only
66                  * bind to those interfaces. Create a socket per interface and
67                  * bind to only these.
68                  */
69
70                 /* Now open a listen socket for each of the interfaces. */
71                 for(i = 0; i < num_ifs; i++) {
72                         const struct sockaddr_storage *ifss =
73                                         iface_n_sockaddr_storage(i);
74
75                         p = setup_dcerpc_ncacn_tcpip_socket(ev_ctx,
76                                                             msg_ctx,
77                                                             syntax_id,
78                                                             ifss,
79                                                             port);
80                         if (p == 0) {
81                                 return 0;
82                         }
83                         port = p;
84                 }
85         } else {
86                 const char *sock_addr = lp_socket_address();
87                 const char *sock_ptr;
88                 char *sock_tok;
89
90                 for (sock_ptr = sock_addr;
91                      next_token_talloc(talloc_tos(), &sock_ptr, &sock_tok, " \t,");
92                     ) {
93                         struct sockaddr_storage ss;
94
95                         /* open an incoming socket */
96                         if (!interpret_string_addr(&ss,
97                                                    sock_tok,
98                                                    AI_NUMERICHOST|AI_PASSIVE)) {
99                                 continue;
100                         }
101
102                         p = setup_dcerpc_ncacn_tcpip_socket(ev_ctx,
103                                                             msg_ctx,
104                                                             syntax_id,
105                                                             &ss,
106                                                             port);
107                         if (p == 0) {
108                                 return 0;
109                         }
110                         port = p;
111                 }
112         }
113
114         return p;
115 }
116
117 static NTSTATUS _rpc_ep_register(struct tevent_context *ev_ctx,
118                                  struct messaging_context *msg_ctx,
119                                  const struct ndr_interface_table *iface,
120                                  const char *name,
121                                  uint16_t port)
122 {
123         struct dcerpc_binding_vector *v = NULL;
124         NTSTATUS status;
125
126         status = dcerpc_binding_vector_create(talloc_tos(),
127                                               iface,
128                                               port,
129                                               name,
130                                               &v);
131         if (!NT_STATUS_IS_OK(status)) {
132                 return status;
133         }
134
135         status = dcerpc_ep_register(iface,
136                                     v,
137                                     &iface->syntax_id.uuid,
138                                     name);
139         if (!NT_STATUS_IS_OK(status)) {
140                 return status;
141         }
142
143         return status;
144 }
145
146 static NTSTATUS _rpc_ep_unregister(const struct ndr_interface_table *iface)
147 {
148         struct dcerpc_binding_vector *v = NULL;
149         NTSTATUS status;
150
151         status = dcerpc_binding_vector_create(talloc_tos(),
152                                               iface,
153                                               0,
154                                               NULL,
155                                               &v);
156         if (!NT_STATUS_IS_OK(status)) {
157                 return status;
158         }
159
160         status = dcerpc_ep_unregister(iface,
161                                       v,
162                                       &iface->syntax_id.uuid);
163         if (!NT_STATUS_IS_OK(status)) {
164                 return status;
165         }
166
167         return status;
168 }
169
170 static bool epmapper_init_cb(void *ptr)
171 {
172         struct dcesrv_ep_context *ep_ctx =
173                 talloc_get_type_abort(ptr, struct dcesrv_ep_context);
174         uint16_t port;
175
176         port = _open_sockets(ep_ctx->ev_ctx,
177                              ep_ctx->msg_ctx,
178                              ndr_table_epmapper.syntax_id,
179                              135);
180         if (port == 135) {
181                 return true;
182         }
183
184         return false;
185 }
186
187 static bool winreg_init_cb(void *ptr)
188 {
189         struct dcesrv_ep_context *ep_ctx =
190                 talloc_get_type_abort(ptr, struct dcesrv_ep_context);
191         struct ndr_syntax_id abstract_syntax = ndr_table_winreg.syntax_id;
192         const char *pipe_name = "winreg";
193         const char *rpcsrv_type;
194         uint16_t port;
195
196         rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
197                                            "rpc_server",
198                                            "epmapper",
199                                            "none");
200
201         if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
202             StrCaseCmp(rpcsrv_type, "daemon") == 0) {
203                 NTSTATUS status;
204                 bool ok;
205
206                 ok = setup_dcerpc_ncalrpc_socket(ep_ctx->ev_ctx,
207                                                  ep_ctx->msg_ctx,
208                                                  abstract_syntax,
209                                                  pipe_name);
210                 if (!ok) {
211                         return false;
212                 }
213                 port = _open_sockets(ep_ctx->ev_ctx,
214                                      ep_ctx->msg_ctx,
215                                      abstract_syntax,
216                                      0);
217                 if (port == 0) {
218                         return false;
219                 }
220
221                 status = _rpc_ep_register(ep_ctx->ev_ctx,
222                                                 ep_ctx->msg_ctx,
223                                                 &ndr_table_winreg,
224                                                 pipe_name,
225                                                 port);
226                 if (!NT_STATUS_IS_OK(status)) {
227                         return false;
228                 }
229         }
230
231         return true;
232 }
233
234 static bool winreg_shutdown_cb(void *ptr)
235 {
236         const char *rpcsrv_type;
237
238         rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
239                                            "rpc_server",
240                                            "epmapper",
241                                            "none");
242
243         if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
244             StrCaseCmp(rpcsrv_type, "daemon") == 0) {
245                 NTSTATUS status;
246
247                 status = _rpc_ep_unregister(&ndr_table_winreg);
248                 if (!NT_STATUS_IS_OK(status)) {
249                         return false;
250                 }
251         }
252
253         return true;
254 }
255
256 static bool srvsvc_init_cb(void *ptr)
257 {
258         struct dcesrv_ep_context *ep_ctx =
259                 talloc_get_type_abort(ptr, struct dcesrv_ep_context);
260         struct ndr_syntax_id abstract_syntax = ndr_table_srvsvc.syntax_id;
261         const char *pipe_name = "srvsvc";
262         const char *rpcsrv_type;
263         uint16_t port;
264
265         rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
266                                            "rpc_server",
267                                            "epmapper",
268                                            "none");
269
270         if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
271             StrCaseCmp(rpcsrv_type, "daemon") == 0) {
272                 NTSTATUS status;
273                 bool ok;
274
275                 ok = setup_dcerpc_ncalrpc_socket(ep_ctx->ev_ctx,
276                                                  ep_ctx->msg_ctx,
277                                                  abstract_syntax,
278                                                  pipe_name);
279                 if (!ok) {
280                         return false;
281                 }
282
283                 port = _open_sockets(ep_ctx->ev_ctx,
284                                      ep_ctx->msg_ctx,
285                                      abstract_syntax,
286                                      0);
287                 if (port == 0) {
288                         return false;
289                 }
290
291                 status = _rpc_ep_register(ep_ctx->ev_ctx,
292                                           ep_ctx->msg_ctx,
293                                           &ndr_table_srvsvc,
294                                           pipe_name,
295                                           port);
296                 if (!NT_STATUS_IS_OK(status)) {
297                         return false;
298                 }
299         }
300
301         return true;
302 }
303
304 static bool srvsvc_shutdown_cb(void *ptr)
305 {
306         const char *rpcsrv_type;
307
308         rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
309                                            "rpc_server",
310                                            "epmapper",
311                                            "none");
312
313         if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
314             StrCaseCmp(rpcsrv_type, "daemon") == 0) {
315                 NTSTATUS status;
316
317                 status =_rpc_ep_unregister(&ndr_table_srvsvc);
318                 if (!NT_STATUS_IS_OK(status)) {
319                         return false;
320                 }
321         }
322
323         return true;
324 }
325
326 static bool lsarpc_init_cb(void *ptr)
327 {
328         struct dcesrv_ep_context *ep_ctx =
329                 talloc_get_type_abort(ptr, struct dcesrv_ep_context);
330         struct ndr_syntax_id abstract_syntax = ndr_table_lsarpc.syntax_id;
331         const char *pipe_name = "lsarpc";
332         const char *rpcsrv_type;
333         uint16_t port;
334
335         rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
336                                            "rpc_server",
337                                            "epmapper",
338                                            "none");
339
340         if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
341             StrCaseCmp(rpcsrv_type, "daemon") == 0) {
342                 NTSTATUS status;
343                 bool ok;
344
345                 ok = setup_dcerpc_ncalrpc_socket(ep_ctx->ev_ctx,
346                                                  ep_ctx->msg_ctx,
347                                                  abstract_syntax,
348                                                  pipe_name);
349                 if (!ok) {
350                         return false;
351                 }
352
353                 port = _open_sockets(ep_ctx->ev_ctx,
354                                      ep_ctx->msg_ctx,
355                                      abstract_syntax,
356                                      0);
357                 if (port == 0) {
358                         return false;
359                 }
360
361                 status = _rpc_ep_register(ep_ctx->ev_ctx,
362                                           ep_ctx->msg_ctx,
363                                           &ndr_table_lsarpc,
364                                           pipe_name,
365                                           port);
366                 if (!NT_STATUS_IS_OK(status)) {
367                         return false;
368                 }
369         }
370
371         return true;
372 }
373
374 static bool lsarpc_shutdown_cb(void *ptr)
375 {
376         const char *rpcsrv_type;
377
378         rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
379                                            "rpc_server",
380                                            "epmapper",
381                                            "none");
382
383         if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
384             StrCaseCmp(rpcsrv_type, "daemon") == 0) {
385                 NTSTATUS status;
386
387                 status = _rpc_ep_unregister(&ndr_table_lsarpc);
388                 if (!NT_STATUS_IS_OK(status)) {
389                         return false;
390                 }
391         }
392
393         return true;
394 }
395
396 static bool samr_init_cb(void *ptr)
397 {
398         struct dcesrv_ep_context *ep_ctx =
399                 talloc_get_type_abort(ptr, struct dcesrv_ep_context);
400         struct ndr_syntax_id abstract_syntax = ndr_table_samr.syntax_id;
401         const char *pipe_name = "samr";
402         const char *rpcsrv_type;
403         uint16_t port;
404
405         rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
406                                            "rpc_server",
407                                            "epmapper",
408                                            "none");
409
410         if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
411             StrCaseCmp(rpcsrv_type, "daemon") == 0) {
412                 NTSTATUS status;
413                 bool ok;
414
415                 ok = setup_dcerpc_ncalrpc_socket(ep_ctx->ev_ctx,
416                                                  ep_ctx->msg_ctx,
417                                                  abstract_syntax,
418                                                  pipe_name);
419                 if (!ok) {
420                         return false;
421                 }
422
423                 port = _open_sockets(ep_ctx->ev_ctx,
424                                      ep_ctx->msg_ctx,
425                                      abstract_syntax,
426                                      0);
427                 if (port == 0) {
428                         return false;
429                 }
430
431                 status = _rpc_ep_register(ep_ctx->ev_ctx,
432                                           ep_ctx->msg_ctx,
433                                           &ndr_table_samr,
434                                           pipe_name,
435                                           port);
436                 if (!NT_STATUS_IS_OK(status)) {
437                         return false;
438                 }
439         }
440
441         return true;
442 }
443
444 static bool samr_shutdown_cb(void *ptr)
445 {
446         const char *rpcsrv_type;
447
448         rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
449                                            "rpc_server",
450                                            "epmapper",
451                                            "none");
452
453         if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
454             StrCaseCmp(rpcsrv_type, "daemon") == 0) {
455                 NTSTATUS status;
456
457                 status = _rpc_ep_unregister(&ndr_table_samr);
458                 if (!NT_STATUS_IS_OK(status)) {
459                         return false;
460                 }
461         }
462
463         return true;
464 }
465
466 static bool netlogon_init_cb(void *ptr)
467 {
468         struct dcesrv_ep_context *ep_ctx =
469                 talloc_get_type_abort(ptr, struct dcesrv_ep_context);
470         struct ndr_syntax_id abstract_syntax = ndr_table_netlogon.syntax_id;
471         const char *pipe_name = "netlogon";
472         const char *rpcsrv_type;
473         uint16_t port;
474
475         rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
476                                            "rpc_server",
477                                            "epmapper",
478                                            "none");
479
480         if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
481             StrCaseCmp(rpcsrv_type, "daemon") == 0) {
482                 NTSTATUS status;
483                 bool ok;
484
485                 ok = setup_dcerpc_ncalrpc_socket(ep_ctx->ev_ctx,
486                                                  ep_ctx->msg_ctx,
487                                                  abstract_syntax,
488                                                  pipe_name);
489                 if (!ok) {
490                         return false;
491                 }
492
493                 port = _open_sockets(ep_ctx->ev_ctx,
494                                      ep_ctx->msg_ctx,
495                                      abstract_syntax,
496                                      0);
497                 if (port == 0) {
498                         return false;
499                 }
500
501                 status = _rpc_ep_register(ep_ctx->ev_ctx,
502                                           ep_ctx->msg_ctx,
503                                           &ndr_table_netlogon,
504                                           pipe_name,
505                                           port);
506                 if (!NT_STATUS_IS_OK(status)) {
507                         return false;
508                 }
509         }
510
511         return true;
512 }
513
514 static bool netlogon_shutdown_cb(void *ptr)
515 {
516         const char *rpcsrv_type;
517
518         rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
519                                            "rpc_server",
520                                            "epmapper",
521                                            "none");
522
523         if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
524             StrCaseCmp(rpcsrv_type, "daemon") == 0) {
525                 NTSTATUS status;
526
527                 status = _rpc_ep_unregister(&ndr_table_netlogon);
528                 if (!NT_STATUS_IS_OK(status)) {
529                         return false;
530                 }
531         }
532
533         return true;
534 }
535
536 static bool spoolss_init_cb(void *ptr)
537 {
538         struct dcesrv_ep_context *ep_ctx =
539                 talloc_get_type_abort(ptr, struct dcesrv_ep_context);
540         const char *rpcsrv_type;
541         bool ok;
542
543         rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
544                                            "rpc_server",
545                                            "epmapper",
546                                            "none");
547
548         /*
549          * Migrate the printers first.
550          */
551         ok = nt_printing_tdb_migrate(ep_ctx->msg_ctx);
552         if (!ok) {
553                 return false;
554         }
555
556         if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
557             StrCaseCmp(rpcsrv_type, "daemon") == 0) {
558                 NTSTATUS status;
559
560                 status =_rpc_ep_register(ep_ctx->ev_ctx,
561                                          ep_ctx->msg_ctx,
562                                          &ndr_table_spoolss,
563                                          "spoolss",
564                                          0);
565                 if (!NT_STATUS_IS_OK(status)) {
566                         return false;
567                 }
568         }
569
570         return true;
571 }
572
573 static bool spoolss_shutdown_cb(void *ptr)
574 {
575         const char *rpcsrv_type;
576
577         rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
578                                            "rpc_server",
579                                            "epmapper",
580                                            "none");
581
582         srv_spoolss_cleanup();
583
584         if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
585             StrCaseCmp(rpcsrv_type, "daemon") == 0) {
586                 NTSTATUS status;
587
588                 status = _rpc_ep_unregister(&ndr_table_spoolss);
589                 if (!NT_STATUS_IS_OK(status)) {
590                         return false;
591                 }
592         }
593
594         return true;
595 }
596
597 static bool svcctl_init_cb(void *ptr)
598 {
599         struct dcesrv_ep_context *ep_ctx =
600                 talloc_get_type_abort(ptr, struct dcesrv_ep_context);
601         const char *rpcsrv_type;
602         bool ok;
603
604         rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
605                                            "rpc_server",
606                                            "epmapper",
607                                            "none");
608
609         ok = svcctl_init_winreg(ep_ctx->msg_ctx);
610         if (!ok) {
611                 return false;
612         }
613
614         /* initialize the control hooks */
615         init_service_op_table();
616
617         if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
618             StrCaseCmp(rpcsrv_type, "daemon") == 0) {
619                 NTSTATUS status;
620
621                 status = _rpc_ep_register(ep_ctx->ev_ctx,
622                                           ep_ctx->msg_ctx,
623                                           &ndr_table_svcctl,
624                                           "svcctl",
625                                           0);
626                 if (!NT_STATUS_IS_OK(status)) {
627                         return false;
628                 }
629         }
630
631         return true;
632 }
633
634 static bool svcctl_shutdown_cb(void *ptr)
635 {
636         const char *rpcsrv_type;
637
638         rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
639                                            "rpc_server",
640                                            "epmapper",
641                                            "none");
642         shutdown_service_op_table();
643
644         if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
645             StrCaseCmp(rpcsrv_type, "daemon") == 0) {
646                 NTSTATUS status;
647
648                 status = _rpc_ep_unregister(&ndr_table_svcctl);
649                 if (!NT_STATUS_IS_OK(status)) {
650                         return false;
651                 }
652         }
653
654         return true;
655 }
656
657 static bool ntsvcs_init_cb(void *ptr)
658 {
659         struct dcesrv_ep_context *ep_ctx =
660                 talloc_get_type_abort(ptr, struct dcesrv_ep_context);
661         const char *rpcsrv_type;
662
663         rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
664                                            "rpc_server",
665                                            "epmapper",
666                                            "none");
667
668         if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
669             StrCaseCmp(rpcsrv_type, "daemon") == 0) {
670                 NTSTATUS status;
671
672                 status = _rpc_ep_register(ep_ctx->ev_ctx,
673                                           ep_ctx->msg_ctx,
674                                           &ndr_table_ntsvcs,
675                                           "ntsvcs",
676                                           0);
677                 if (!NT_STATUS_IS_OK(status)) {
678                         return false;
679                 }
680         }
681
682         return true;
683 }
684
685 static bool ntsvcs_shutdown_cb(void *ptr)
686 {
687         const char *rpcsrv_type;
688
689         rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
690                                            "rpc_server",
691                                            "epmapper",
692                                            "none");
693
694         if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
695             StrCaseCmp(rpcsrv_type, "daemon") == 0) {
696                 NTSTATUS status;
697
698                 status = _rpc_ep_unregister(&ndr_table_ntsvcs);
699                 if (!NT_STATUS_IS_OK(status)) {
700                         return false;
701                 }
702         }
703
704         return true;
705 }
706
707 static bool eventlog_init_cb(void *ptr)
708 {
709         struct dcesrv_ep_context *ep_ctx =
710                 talloc_get_type_abort(ptr, struct dcesrv_ep_context);
711         const char *rpcsrv_type;
712         bool ok;
713
714         rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
715                                            "rpc_server",
716                                            "epmapper",
717                                            "none");
718
719         ok = eventlog_init_winreg(ep_ctx->msg_ctx);
720         if (!ok) {
721                 return false;
722         }
723
724         if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
725             StrCaseCmp(rpcsrv_type, "daemon") == 0) {
726                 NTSTATUS status;
727
728                 status =_rpc_ep_register(ep_ctx->ev_ctx,
729                                          ep_ctx->msg_ctx,
730                                          &ndr_table_eventlog,
731                                          "eventlog",
732                                          0);
733                 if (!NT_STATUS_IS_OK(status)) {
734                         return false;
735                 }
736         }
737
738         return true;
739 }
740
741 static bool eventlog_shutdown_cb(void *ptr)
742 {
743         const char *rpcsrv_type;
744
745         rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
746                                            "rpc_server",
747                                            "epmapper",
748                                            "none");
749
750         if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
751             StrCaseCmp(rpcsrv_type, "daemon") == 0) {
752                 NTSTATUS status;
753
754                 status = _rpc_ep_unregister(&ndr_table_eventlog);
755                 if (!NT_STATUS_IS_OK(status)) {
756                         return false;
757                 }
758         }
759
760         return true;
761 }
762
763 static bool initshutdown_init_cb(void *ptr)
764 {
765         struct dcesrv_ep_context *ep_ctx =
766                 talloc_get_type_abort(ptr, struct dcesrv_ep_context);
767         const char *rpcsrv_type;
768
769         rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
770                                            "rpc_server",
771                                            "epmapper",
772                                            "none");
773
774         if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
775             StrCaseCmp(rpcsrv_type, "daemon") == 0) {
776                 NTSTATUS status;
777
778                 status = _rpc_ep_register(ep_ctx->ev_ctx,
779                                           ep_ctx->msg_ctx,
780                                           &ndr_table_initshutdown,
781                                           "initshutdown",
782                                           0);
783                 if (!NT_STATUS_IS_OK(status)) {
784                         return false;
785                 }
786         }
787
788         return true;
789 }
790
791 static bool initshutdown_shutdown_cb(void *ptr)
792 {
793         const char *rpcsrv_type;
794
795         rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
796                                            "rpc_server",
797                                            "epmapper",
798                                            "none");
799
800         if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
801             StrCaseCmp(rpcsrv_type, "daemon") == 0) {
802                 NTSTATUS status;
803
804                 status = _rpc_ep_unregister(&ndr_table_initshutdown);
805                 if (!NT_STATUS_IS_OK(status)) {
806                         return false;
807                 }
808         }
809
810         return true;
811 }
812
813 #ifdef DEVELOPER
814 static bool rpcecho_init_cb(void *ptr) {
815         struct dcesrv_ep_context *ep_ctx =
816                 talloc_get_type_abort(ptr, struct dcesrv_ep_context);
817         const char *rpcsrv_type;
818         uint16_t port;
819
820         rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
821                                            "rpc_server",
822                                            "epmapper",
823                                            "none");
824
825         if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
826             StrCaseCmp(rpcsrv_type, "daemon") == 0) {
827                 NTSTATUS status;
828
829                 port = _open_sockets(ep_ctx->ev_ctx,
830                                      ep_ctx->msg_ctx,
831                                      ndr_table_rpcecho.syntax_id,
832                                      0);
833                 if (port == 0) {
834                         return false;
835                 }
836
837                 status = _rpc_ep_register(ep_ctx->ev_ctx,
838                                           ep_ctx->msg_ctx,
839                                           &ndr_table_rpcecho,
840                                           "rpcecho",
841                                           port);
842                 if (!NT_STATUS_IS_OK(status)) {
843                         return false;
844                 }
845         }
846
847         return true;
848 }
849
850 static bool rpcecho_shutdown_cb(void *ptr)
851 {
852         const char *rpcsrv_type;
853
854         rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
855                                            "rpc_server",
856                                            "epmapper",
857                                            "none");
858
859         if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
860             StrCaseCmp(rpcsrv_type, "daemon") == 0) {
861                 NTSTATUS status;
862
863                 status = _rpc_ep_unregister(&ndr_table_rpcecho);
864                 if (!NT_STATUS_IS_OK(status)) {
865                         return false;
866                 }
867         }
868
869         return true;
870 }
871 #endif
872
873 static bool netdfs_init_cb(void *ptr)
874 {
875         struct dcesrv_ep_context *ep_ctx =
876                 talloc_get_type_abort(ptr, struct dcesrv_ep_context);
877         struct ndr_syntax_id abstract_syntax = ndr_table_netdfs.syntax_id;
878         const char *pipe_name = "netdfs";
879         const char *rpcsrv_type;
880         uint16_t port;
881
882         rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
883                                            "rpc_server",
884                                            "epmapper",
885                                            "none");
886         if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
887             StrCaseCmp(rpcsrv_type, "daemon") == 0) {
888                 NTSTATUS status;
889                 bool ok;
890
891                 ok = setup_dcerpc_ncalrpc_socket(ep_ctx->ev_ctx,
892                                                  ep_ctx->msg_ctx,
893                                                  abstract_syntax,
894                                                  pipe_name);
895                 if (!ok) {
896                         return false;
897                 }
898
899                 port = _open_sockets(ep_ctx->ev_ctx,
900                                      ep_ctx->msg_ctx,
901                                      abstract_syntax,
902                                      0);
903                 if (port == 0) {
904                         return false;
905                 }
906
907                 status = _rpc_ep_register(ep_ctx->ev_ctx,
908                                           ep_ctx->msg_ctx,
909                                           &ndr_table_netdfs,
910                                           pipe_name,
911                                           port);
912                 if (!NT_STATUS_IS_OK(status)) {
913                         return false;
914                 }
915         }
916
917         return true;
918 }
919
920 static bool netdfs_shutdown_cb(void *ptr) {
921         const char *rpcsrv_type;
922
923         rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
924                                            "rpc_server",
925                                            "epmapper",
926                                            "none");
927
928         if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
929             StrCaseCmp(rpcsrv_type, "daemon") == 0) {
930                 NTSTATUS status;
931
932                 status = _rpc_ep_unregister(&ndr_table_netdfs);
933                 if (!NT_STATUS_IS_OK(status)) {
934                         return false;
935                 }
936         }
937
938         return true;
939 }
940
941 static bool dssetup_init_cb(void *ptr)
942 {
943         struct dcesrv_ep_context *ep_ctx =
944                 talloc_get_type_abort(ptr, struct dcesrv_ep_context);
945         struct ndr_syntax_id abstract_syntax = ndr_table_dssetup.syntax_id;
946         const char *pipe_name = "dssetup";
947         const char *rpcsrv_type;
948         uint16_t port;
949
950         rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
951                                            "rpc_server",
952                                            "epmapper",
953                                            "none");
954
955         if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
956             StrCaseCmp(rpcsrv_type, "daemon") == 0) {
957                 NTSTATUS status;
958                 bool ok;
959
960                 ok = setup_dcerpc_ncalrpc_socket(ep_ctx->ev_ctx,
961                                                  ep_ctx->msg_ctx,
962                                                  abstract_syntax,
963                                                  pipe_name);
964                 if (!ok) {
965                         return false;
966                 }
967
968                 port = _open_sockets(ep_ctx->ev_ctx,
969                                      ep_ctx->msg_ctx,
970                                      abstract_syntax,
971                                      0);
972                 if (port == 0) {
973                         return false;
974                 }
975
976                 status = _rpc_ep_register(ep_ctx->ev_ctx,
977                                           ep_ctx->msg_ctx,
978                                           &ndr_table_dssetup,
979                                           "dssetup",
980                                           port);
981                 if (!NT_STATUS_IS_OK(status)) {
982                         return false;
983                 }
984         }
985
986         return true;
987 }
988
989 static bool dssetup_shutdown_cb(void *ptr) {
990         const char *rpcsrv_type;
991
992         rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
993                                            "rpc_server",
994                                            "epmapper",
995                                            "none");
996
997         if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
998             StrCaseCmp(rpcsrv_type, "daemon") == 0) {
999                 NTSTATUS status;
1000
1001                 status = _rpc_ep_unregister(&ndr_table_dssetup);
1002                 if (!NT_STATUS_IS_OK(status)) {
1003                         return false;
1004                 }
1005         }
1006
1007         return true;
1008 }
1009
1010 static bool wkssvc_init_cb(void *ptr)
1011 {
1012         struct dcesrv_ep_context *ep_ctx =
1013                 talloc_get_type_abort(ptr, struct dcesrv_ep_context);
1014         struct ndr_syntax_id abstract_syntax = ndr_table_wkssvc.syntax_id;
1015         const char *pipe_name = "wkssvc";
1016         const char *rpcsrv_type;
1017         uint16_t port;
1018
1019         rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
1020                                            "rpc_server",
1021                                            "epmapper",
1022                                            "none");
1023         if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
1024             StrCaseCmp(rpcsrv_type, "daemon") == 0) {
1025                 NTSTATUS status;
1026                 bool ok;
1027
1028                 ok = setup_dcerpc_ncalrpc_socket(ep_ctx->ev_ctx,
1029                                                  ep_ctx->msg_ctx,
1030                                                  abstract_syntax,
1031                                                  pipe_name);
1032                 if (!ok) {
1033                         return false;
1034                 }
1035
1036                 port = _open_sockets(ep_ctx->ev_ctx,
1037                                      ep_ctx->msg_ctx,
1038                                      abstract_syntax,
1039                                      0);
1040                 if (port == 0) {
1041                         return false;
1042                 }
1043
1044                 status = _rpc_ep_register(ep_ctx->ev_ctx,
1045                                           ep_ctx->msg_ctx,
1046                                           &ndr_table_wkssvc,
1047                                           "wkssvc",
1048                                           port);
1049                 if (!NT_STATUS_IS_OK(status)) {
1050                         return false;
1051                 }
1052         }
1053
1054         return true;
1055 }
1056
1057 static bool wkssvc_shutdown_cb(void *ptr) {
1058         return NT_STATUS_IS_OK(_rpc_ep_unregister(&ndr_table_wkssvc));
1059 }
1060
1061 bool dcesrv_ep_setup(struct tevent_context *ev_ctx,
1062                      struct messaging_context *msg_ctx)
1063 {
1064         struct dcesrv_ep_context *ep_ctx;
1065
1066         struct rpc_srv_callbacks epmapper_cb;
1067
1068         struct rpc_srv_callbacks winreg_cb;
1069         struct rpc_srv_callbacks srvsvc_cb;
1070
1071         struct rpc_srv_callbacks lsarpc_cb;
1072         struct rpc_srv_callbacks samr_cb;
1073         struct rpc_srv_callbacks netlogon_cb;
1074
1075         struct rpc_srv_callbacks spoolss_cb;
1076         struct rpc_srv_callbacks svcctl_cb;
1077         struct rpc_srv_callbacks ntsvcs_cb;
1078         struct rpc_srv_callbacks eventlog_cb;
1079         struct rpc_srv_callbacks initshutdown_cb;
1080         struct rpc_srv_callbacks netdfs_cb;
1081 #ifdef DEVELOPER
1082         struct rpc_srv_callbacks rpcecho_cb;
1083 #endif
1084         struct rpc_srv_callbacks dssetup_cb;
1085         struct rpc_srv_callbacks wkssvc_cb;
1086
1087         const char *rpcsrv_type;
1088
1089         ep_ctx = talloc(ev_ctx, struct dcesrv_ep_context);
1090         if (ep_ctx == NULL) {
1091                 return false;
1092         }
1093
1094         ep_ctx->ev_ctx = ev_ctx;
1095         ep_ctx->msg_ctx = msg_ctx;
1096
1097         /* start endpoint mapper only if enabled */
1098         rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
1099                                            "rpc_server",
1100                                            "epmapper",
1101                                            "none");
1102         if (StrCaseCmp(rpcsrv_type, "embedded") == 0) {
1103                 epmapper_cb.init         = epmapper_init_cb;
1104                 epmapper_cb.shutdown     = NULL;
1105                 epmapper_cb.private_data = ep_ctx;
1106
1107                 if (!NT_STATUS_IS_OK(rpc_epmapper_init(&epmapper_cb))) {
1108                         return false;
1109                 }
1110         }
1111
1112         winreg_cb.init         = winreg_init_cb;
1113         winreg_cb.shutdown     = winreg_shutdown_cb;
1114         winreg_cb.private_data = ep_ctx;
1115         if (!NT_STATUS_IS_OK(rpc_winreg_init(&winreg_cb))) {
1116                 return false;
1117         }
1118
1119         srvsvc_cb.init         = srvsvc_init_cb;
1120         srvsvc_cb.shutdown     = srvsvc_shutdown_cb;
1121         srvsvc_cb.private_data = ep_ctx;
1122         if (!NT_STATUS_IS_OK(rpc_srvsvc_init(&srvsvc_cb))) {
1123                 return false;
1124         }
1125
1126
1127         lsarpc_cb.init         = lsarpc_init_cb;
1128         lsarpc_cb.shutdown     = lsarpc_shutdown_cb;
1129         lsarpc_cb.private_data = ep_ctx;
1130         if (!NT_STATUS_IS_OK(rpc_lsarpc_init(&lsarpc_cb))) {
1131                 return false;
1132         }
1133
1134         samr_cb.init         = samr_init_cb;
1135         samr_cb.shutdown     = samr_shutdown_cb;
1136         samr_cb.private_data = ep_ctx;
1137         if (!NT_STATUS_IS_OK(rpc_samr_init(&samr_cb))) {
1138                 return false;
1139         }
1140
1141         netlogon_cb.init         = netlogon_init_cb;
1142         netlogon_cb.shutdown     = netlogon_shutdown_cb;
1143         netlogon_cb.private_data = ep_ctx;
1144         if (!NT_STATUS_IS_OK(rpc_netlogon_init(&netlogon_cb))) {
1145                 return false;
1146         }
1147
1148         spoolss_cb.init         = spoolss_init_cb;
1149         spoolss_cb.shutdown     = spoolss_shutdown_cb;
1150         spoolss_cb.private_data = ep_ctx;
1151         if (!NT_STATUS_IS_OK(rpc_spoolss_init(&spoolss_cb))) {
1152                 return false;
1153         }
1154
1155
1156         svcctl_cb.init         = svcctl_init_cb;
1157         svcctl_cb.shutdown     = svcctl_shutdown_cb;
1158         svcctl_cb.private_data = ep_ctx;
1159         if (!NT_STATUS_IS_OK(rpc_svcctl_init(&svcctl_cb))) {
1160                 return false;
1161         }
1162
1163         ntsvcs_cb.init         = ntsvcs_init_cb;
1164         ntsvcs_cb.shutdown     = ntsvcs_shutdown_cb;
1165         ntsvcs_cb.private_data = ep_ctx;
1166         if (!NT_STATUS_IS_OK(rpc_ntsvcs_init(&ntsvcs_cb))) {
1167                 return false;
1168         }
1169
1170         eventlog_cb.init         = eventlog_init_cb;
1171         eventlog_cb.shutdown     = eventlog_shutdown_cb;
1172         eventlog_cb.private_data = ep_ctx;
1173         if (!NT_STATUS_IS_OK(rpc_eventlog_init(&eventlog_cb))) {
1174                 return false;
1175         }
1176
1177         initshutdown_cb.init         = initshutdown_init_cb;
1178         initshutdown_cb.shutdown     = initshutdown_shutdown_cb;
1179         initshutdown_cb.private_data = ep_ctx;
1180         if (!NT_STATUS_IS_OK(rpc_initshutdown_init(&initshutdown_cb))) {
1181                 return false;
1182         }
1183
1184         netdfs_cb.init         = netdfs_init_cb;
1185         netdfs_cb.shutdown     = netdfs_shutdown_cb;
1186         netdfs_cb.private_data = ep_ctx;
1187         if (!NT_STATUS_IS_OK(rpc_netdfs_init(&netdfs_cb))) {
1188                 return false;
1189         }
1190 #ifdef DEVELOPER
1191
1192         rpcecho_cb.init         = rpcecho_init_cb;
1193         rpcecho_cb.shutdown     = rpcecho_shutdown_cb;
1194         rpcecho_cb.private_data = ep_ctx;
1195         if (!NT_STATUS_IS_OK(rpc_rpcecho_init(&rpcecho_cb))) {
1196                 return false;
1197         }
1198 #endif
1199
1200         dssetup_cb.init         = dssetup_init_cb;
1201         dssetup_cb.shutdown     = dssetup_shutdown_cb;
1202         dssetup_cb.private_data = ep_ctx;
1203         if (!NT_STATUS_IS_OK(rpc_dssetup_init(&dssetup_cb))) {
1204                 return false;
1205         }
1206
1207         wkssvc_cb.init         = wkssvc_init_cb;
1208         wkssvc_cb.shutdown     = wkssvc_shutdown_cb;
1209         wkssvc_cb.private_data = ep_ctx;
1210         if (!NT_STATUS_IS_OK(rpc_wkssvc_init(&wkssvc_cb))) {
1211                 return false;
1212         }
1213
1214         return true;
1215 }
1216
1217 /* vim: set ts=8 sw=8 noet cindent ft=c.doxygen: */