s3-rpc_server: Implement an endpoint monitor loop.
[Samba/vl.git] / source3 / rpc_server / rpc_ep_setup.c
blob836ef3024203819719c329b1221a930eb5aa01a8
1 /*
2 * Unix SMB/CIFS implementation.
4 * SMBD RPC service callbacks
6 * Copyright (c) 2011 Andreas Schneider <asn@samba.org>
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.
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.
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/>.
22 #include "includes.h"
24 #include "../librpc/gen_ndr/ndr_epmapper_c.h"
25 #include "../librpc/gen_ndr/srv_epmapper.h"
26 #include "../librpc/gen_ndr/srv_srvsvc.h"
27 #include "../librpc/gen_ndr/srv_winreg.h"
28 #include "../librpc/gen_ndr/srv_dfs.h"
29 #include "../librpc/gen_ndr/srv_dssetup.h"
30 #include "../librpc/gen_ndr/srv_echo.h"
31 #include "../librpc/gen_ndr/srv_eventlog.h"
32 #include "../librpc/gen_ndr/srv_initshutdown.h"
33 #include "../librpc/gen_ndr/srv_lsa.h"
34 #include "../librpc/gen_ndr/srv_netlogon.h"
35 #include "../librpc/gen_ndr/srv_ntsvcs.h"
36 #include "../librpc/gen_ndr/srv_samr.h"
37 #include "../librpc/gen_ndr/srv_spoolss.h"
38 #include "../librpc/gen_ndr/srv_svcctl.h"
39 #include "../librpc/gen_ndr/srv_wkssvc.h"
41 #include "printing/nt_printing_migrate.h"
42 #include "rpc_server/eventlog/srv_eventlog_reg.h"
43 #include "rpc_server/svcctl/srv_svcctl_reg.h"
45 #include "librpc/rpc/dcerpc_ep.h"
47 #include "rpc_server/rpc_ep_setup.h"
48 #include "rpc_server/rpc_server.h"
49 #include "rpc_server/epmapper/srv_epmapper.h"
51 struct dcesrv_ep_context {
52 struct tevent_context *ev_ctx;
53 struct messaging_context *msg_ctx;
56 static uint16_t _open_sockets(struct tevent_context *ev_ctx,
57 struct messaging_context *msg_ctx,
58 struct ndr_syntax_id syntax_id,
59 uint16_t port)
61 uint32_t num_ifs = iface_count();
62 uint32_t i;
63 uint16_t p = 0;
65 if (lp_interfaces() && lp_bind_interfaces_only()) {
67 * We have been given an interfaces line, and been told to only
68 * bind to those interfaces. Create a socket per interface and
69 * bind to only these.
72 /* Now open a listen socket for each of the interfaces. */
73 for(i = 0; i < num_ifs; i++) {
74 const struct sockaddr_storage *ifss =
75 iface_n_sockaddr_storage(i);
77 p = setup_dcerpc_ncacn_tcpip_socket(ev_ctx,
78 msg_ctx,
79 syntax_id,
80 ifss,
81 port);
82 if (p == 0) {
83 return 0;
85 port = p;
87 } else {
88 const char *sock_addr = lp_socket_address();
89 const char *sock_ptr;
90 char *sock_tok;
92 if (strequal(sock_addr, "0.0.0.0") ||
93 strequal(sock_addr, "::")) {
94 #if HAVE_IPV6
95 sock_addr = "::";
96 #else
97 sock_addr = "0.0.0.0";
98 #endif
101 for (sock_ptr = sock_addr;
102 next_token_talloc(talloc_tos(), &sock_ptr, &sock_tok, " \t,");
104 struct sockaddr_storage ss;
106 /* open an incoming socket */
107 if (!interpret_string_addr(&ss,
108 sock_tok,
109 AI_NUMERICHOST|AI_PASSIVE)) {
110 continue;
113 p = setup_dcerpc_ncacn_tcpip_socket(ev_ctx,
114 msg_ctx,
115 syntax_id,
116 &ss,
117 port);
118 if (p == 0) {
119 return 0;
121 port = p;
125 return p;
128 static NTSTATUS _rpc_ep_unregister(const struct ndr_interface_table *iface)
130 struct dcerpc_binding_vector *v = NULL;
131 NTSTATUS status;
133 status = dcerpc_binding_vector_create(talloc_tos(),
134 iface,
136 NULL,
137 &v);
138 if (!NT_STATUS_IS_OK(status)) {
139 return status;
142 status = dcerpc_ep_unregister(iface,
144 &iface->syntax_id.uuid);
145 if (!NT_STATUS_IS_OK(status)) {
146 return status;
149 return status;
152 static void rpc_ep_setup_register_loop(struct tevent_req *subreq);
153 static NTSTATUS rpc_ep_setup_try_register(TALLOC_CTX *mem_ctx,
154 struct tevent_context *ev_ctx,
155 struct messaging_context *msg_ctx,
156 const struct ndr_interface_table *iface,
157 const char *name,
158 uint16_t port,
159 struct dcerpc_binding_handle **pbh);
161 struct rpc_ep_regsiter_state {
162 struct dcerpc_binding_handle *h;
164 TALLOC_CTX *mem_ctx;
165 struct tevent_context *ev_ctx;
166 struct messaging_context *msg_ctx;
168 const struct ndr_interface_table *iface;
170 const char *ncalrpc;
171 uint16_t port;
173 uint32_t wait_time;
176 static NTSTATUS rpc_ep_setup_register(struct tevent_context *ev_ctx,
177 struct messaging_context *msg_ctx,
178 const struct ndr_interface_table *iface,
179 const char *ncalrpc,
180 uint16_t port)
182 struct rpc_ep_regsiter_state *state;
183 struct tevent_req *req;
185 state = talloc(ev_ctx, struct rpc_ep_regsiter_state);
186 if (state == NULL) {
187 return NT_STATUS_NO_MEMORY;
190 state->mem_ctx = talloc_named(state,
192 "ep %s %p",
193 iface->name, state);
194 if (state->mem_ctx == NULL) {
195 talloc_free(state);
196 return NT_STATUS_NO_MEMORY;
199 state->wait_time = 1;
200 state->ev_ctx = ev_ctx;
201 state->msg_ctx = msg_ctx;
202 state->iface = iface;
203 state->ncalrpc = talloc_strdup(state, ncalrpc);
204 state->port = port;
206 req = tevent_wakeup_send(state->mem_ctx,
207 state->ev_ctx,
208 timeval_current_ofs(1, 0));
209 if (tevent_req_nomem(state->mem_ctx, req)) {
210 talloc_free(state);
211 return NT_STATUS_NO_MEMORY;
214 tevent_req_set_callback(req, rpc_ep_setup_register_loop, state);
216 return NT_STATUS_OK;
219 #define MONITOR_WAIT_TIME 15
220 static void rpc_ep_setup_monitor_loop(struct tevent_req *subreq);
222 static void rpc_ep_setup_register_loop(struct tevent_req *req)
224 struct rpc_ep_regsiter_state *state =
225 tevent_req_callback_data(req, struct rpc_ep_regsiter_state);
226 NTSTATUS status;
227 bool ok;
229 ok = tevent_wakeup_recv(req);
230 TALLOC_FREE(req);
231 if (!ok) {
232 talloc_free(state);
233 return;
236 status = rpc_ep_setup_try_register(state->mem_ctx,
237 state->ev_ctx,
238 state->msg_ctx,
239 state->iface,
240 state->ncalrpc,
241 state->port,
242 &state->h);
243 if (NT_STATUS_IS_OK(status)) {
244 /* endpoint registered, monitor the connnection. */
245 req = tevent_wakeup_send(state->mem_ctx,
246 state->ev_ctx,
247 timeval_current_ofs(MONITOR_WAIT_TIME, 0));
248 if (tevent_req_nomem(state->mem_ctx, req)) {
249 talloc_free(state);
250 return;
253 tevent_req_set_callback(req, rpc_ep_setup_monitor_loop, state);
254 return;
257 state->wait_time = state->wait_time * 2;
258 if (state->wait_time > 16) {
259 talloc_free(state);
260 return;
263 req = tevent_wakeup_send(state->mem_ctx,
264 state->ev_ctx,
265 timeval_current_ofs(state->wait_time, 0));
266 if (tevent_req_nomem(state->mem_ctx, req)) {
267 talloc_free(state);
268 return;
271 tevent_req_set_callback(req, rpc_ep_setup_register_loop, state);
272 return;
275 static NTSTATUS rpc_ep_setup_try_register(TALLOC_CTX *mem_ctx,
276 struct tevent_context *ev_ctx,
277 struct messaging_context *msg_ctx,
278 const struct ndr_interface_table *iface,
279 const char *name,
280 uint16_t port,
281 struct dcerpc_binding_handle **pbh)
283 struct dcerpc_binding_vector *v = NULL;
284 NTSTATUS status;
286 status = dcerpc_binding_vector_create(mem_ctx,
287 iface,
288 port,
289 name,
290 &v);
291 if (!NT_STATUS_IS_OK(status)) {
292 return status;
295 status = dcerpc_ep_register(mem_ctx,
296 iface,
298 &iface->syntax_id.uuid,
299 name,
300 pbh);
301 talloc_free(v);
302 if (!NT_STATUS_IS_OK(status)) {
303 return status;
306 return status;
310 * Monitor the connection to the endpoint mapper and if it goes away, try to
311 * register the endpoint.
313 static void rpc_ep_setup_monitor_loop(struct tevent_req *subreq)
315 struct rpc_ep_regsiter_state *state =
316 tevent_req_callback_data(subreq, struct rpc_ep_regsiter_state);
317 struct policy_handle entry_handle;
318 struct dcerpc_binding map_binding;
319 struct epm_twr_p_t towers[10];
320 struct epm_twr_t *map_tower;
321 uint32_t num_towers = 0;
322 struct GUID object;
323 NTSTATUS status;
324 uint32_t result = EPMAPPER_STATUS_CANT_PERFORM_OP;
325 TALLOC_CTX *tmp_ctx;
326 bool ok;
328 ZERO_STRUCT(object);
329 ZERO_STRUCT(entry_handle);
331 tmp_ctx = talloc_stackframe();
332 if (tmp_ctx == NULL) {
333 talloc_free(state);
334 return;
337 ok = tevent_wakeup_recv(subreq);
338 TALLOC_FREE(subreq);
339 if (!ok) {
340 talloc_free(state);
341 return;
344 /* Create map tower */
345 map_binding.transport = NCACN_NP;
346 map_binding.object = state->iface->syntax_id;
347 map_binding.host = "";
348 map_binding.endpoint = "";
350 map_tower = talloc_zero(tmp_ctx, struct epm_twr_t);
351 if (map_tower == NULL) {
352 talloc_free(tmp_ctx);
353 talloc_free(state);
354 return;
357 status = dcerpc_binding_build_tower(map_tower, &map_binding,
358 &map_tower->tower);
359 if (!NT_STATUS_IS_OK(status)) {
360 talloc_free(tmp_ctx);
361 talloc_free(state);
362 return;
365 ok = false;
366 status = dcerpc_epm_Map(state->h,
367 tmp_ctx,
368 &object,
369 map_tower,
370 &entry_handle,
372 &num_towers,
373 towers,
374 &result);
375 if (NT_STATUS_IS_OK(status)) {
376 ok = true;
378 if (result == EPMAPPER_STATUS_OK ||
379 result == EPMAPPER_STATUS_NO_MORE_ENTRIES) {
380 ok = true;
382 if (num_towers == 0) {
383 ok = false;
386 talloc_free(tmp_ctx);
388 subreq = tevent_wakeup_send(state->mem_ctx,
389 state->ev_ctx,
390 timeval_current_ofs(MONITOR_WAIT_TIME, 0));
391 if (tevent_req_nomem(state->mem_ctx, subreq)) {
392 talloc_free(state);
393 return;
396 if (ok) {
397 tevent_req_set_callback(subreq, rpc_ep_setup_monitor_loop, state);
398 } else {
399 TALLOC_FREE(state->h);
400 state->wait_time = 1;
402 tevent_req_set_callback(subreq, rpc_ep_setup_register_loop, state);
405 return;
408 static bool epmapper_init_cb(void *ptr)
410 struct dcesrv_ep_context *ep_ctx =
411 talloc_get_type_abort(ptr, struct dcesrv_ep_context);
412 uint16_t port;
414 port = _open_sockets(ep_ctx->ev_ctx,
415 ep_ctx->msg_ctx,
416 ndr_table_epmapper.syntax_id,
417 135);
418 if (port == 135) {
419 return true;
422 return false;
425 static bool epmapper_shutdown_cb(void *ptr)
427 srv_epmapper_cleanup();
429 return true;
432 static bool winreg_init_cb(void *ptr)
434 struct dcesrv_ep_context *ep_ctx =
435 talloc_get_type_abort(ptr, struct dcesrv_ep_context);
436 struct ndr_syntax_id abstract_syntax = ndr_table_winreg.syntax_id;
437 const char *pipe_name = "winreg";
438 const char *rpcsrv_type;
439 uint16_t port;
441 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
442 "rpc_server",
443 "epmapper",
444 "none");
446 if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
447 StrCaseCmp(rpcsrv_type, "daemon") == 0) {
448 NTSTATUS status;
449 bool ok;
451 ok = setup_dcerpc_ncalrpc_socket(ep_ctx->ev_ctx,
452 ep_ctx->msg_ctx,
453 abstract_syntax,
454 pipe_name);
455 if (!ok) {
456 return false;
458 port = _open_sockets(ep_ctx->ev_ctx,
459 ep_ctx->msg_ctx,
460 abstract_syntax,
462 if (port == 0) {
463 return false;
466 status = rpc_ep_setup_register(ep_ctx->ev_ctx,
467 ep_ctx->msg_ctx,
468 &ndr_table_winreg,
469 pipe_name,
470 port);
471 if (!NT_STATUS_IS_OK(status)) {
472 return false;
476 return true;
479 static bool winreg_shutdown_cb(void *ptr)
481 const char *rpcsrv_type;
483 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
484 "rpc_server",
485 "epmapper",
486 "none");
488 if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
489 StrCaseCmp(rpcsrv_type, "daemon") == 0) {
490 NTSTATUS status;
492 status = _rpc_ep_unregister(&ndr_table_winreg);
493 if (!NT_STATUS_IS_OK(status)) {
494 return false;
498 return true;
501 static bool srvsvc_init_cb(void *ptr)
503 struct dcesrv_ep_context *ep_ctx =
504 talloc_get_type_abort(ptr, struct dcesrv_ep_context);
505 struct ndr_syntax_id abstract_syntax = ndr_table_srvsvc.syntax_id;
506 const char *pipe_name = "srvsvc";
507 const char *rpcsrv_type;
508 uint16_t port;
510 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
511 "rpc_server",
512 "epmapper",
513 "none");
515 if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
516 StrCaseCmp(rpcsrv_type, "daemon") == 0) {
517 NTSTATUS status;
518 bool ok;
520 ok = setup_dcerpc_ncalrpc_socket(ep_ctx->ev_ctx,
521 ep_ctx->msg_ctx,
522 abstract_syntax,
523 pipe_name);
524 if (!ok) {
525 return false;
528 port = _open_sockets(ep_ctx->ev_ctx,
529 ep_ctx->msg_ctx,
530 abstract_syntax,
532 if (port == 0) {
533 return false;
536 status = rpc_ep_setup_register(ep_ctx->ev_ctx,
537 ep_ctx->msg_ctx,
538 &ndr_table_srvsvc,
539 pipe_name,
540 port);
541 if (!NT_STATUS_IS_OK(status)) {
542 return false;
546 return true;
549 static bool srvsvc_shutdown_cb(void *ptr)
551 const char *rpcsrv_type;
553 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
554 "rpc_server",
555 "epmapper",
556 "none");
558 if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
559 StrCaseCmp(rpcsrv_type, "daemon") == 0) {
560 NTSTATUS status;
562 status =_rpc_ep_unregister(&ndr_table_srvsvc);
563 if (!NT_STATUS_IS_OK(status)) {
564 return false;
568 return true;
571 static bool lsarpc_init_cb(void *ptr)
573 struct dcesrv_ep_context *ep_ctx =
574 talloc_get_type_abort(ptr, struct dcesrv_ep_context);
575 struct ndr_syntax_id abstract_syntax = ndr_table_lsarpc.syntax_id;
576 const char *pipe_name = "lsarpc";
577 const char *rpcsrv_type;
578 uint16_t port;
580 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
581 "rpc_server",
582 "epmapper",
583 "none");
585 if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
586 StrCaseCmp(rpcsrv_type, "daemon") == 0) {
587 NTSTATUS status;
588 bool ok;
590 ok = setup_dcerpc_ncalrpc_socket(ep_ctx->ev_ctx,
591 ep_ctx->msg_ctx,
592 abstract_syntax,
593 pipe_name);
594 if (!ok) {
595 return false;
598 port = _open_sockets(ep_ctx->ev_ctx,
599 ep_ctx->msg_ctx,
600 abstract_syntax,
602 if (port == 0) {
603 return false;
606 status = rpc_ep_setup_register(ep_ctx->ev_ctx,
607 ep_ctx->msg_ctx,
608 &ndr_table_lsarpc,
609 pipe_name,
610 port);
611 if (!NT_STATUS_IS_OK(status)) {
612 return false;
616 return true;
619 static bool lsarpc_shutdown_cb(void *ptr)
621 const char *rpcsrv_type;
623 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
624 "rpc_server",
625 "epmapper",
626 "none");
628 if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
629 StrCaseCmp(rpcsrv_type, "daemon") == 0) {
630 NTSTATUS status;
632 status = _rpc_ep_unregister(&ndr_table_lsarpc);
633 if (!NT_STATUS_IS_OK(status)) {
634 return false;
638 return true;
641 static bool samr_init_cb(void *ptr)
643 struct dcesrv_ep_context *ep_ctx =
644 talloc_get_type_abort(ptr, struct dcesrv_ep_context);
645 struct ndr_syntax_id abstract_syntax = ndr_table_samr.syntax_id;
646 const char *pipe_name = "samr";
647 const char *rpcsrv_type;
648 uint16_t port;
650 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
651 "rpc_server",
652 "epmapper",
653 "none");
655 if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
656 StrCaseCmp(rpcsrv_type, "daemon") == 0) {
657 NTSTATUS status;
658 bool ok;
660 ok = setup_dcerpc_ncalrpc_socket(ep_ctx->ev_ctx,
661 ep_ctx->msg_ctx,
662 abstract_syntax,
663 pipe_name);
664 if (!ok) {
665 return false;
668 port = _open_sockets(ep_ctx->ev_ctx,
669 ep_ctx->msg_ctx,
670 abstract_syntax,
672 if (port == 0) {
673 return false;
676 status = rpc_ep_setup_register(ep_ctx->ev_ctx,
677 ep_ctx->msg_ctx,
678 &ndr_table_samr,
679 pipe_name,
680 port);
681 if (!NT_STATUS_IS_OK(status)) {
682 return false;
686 return true;
689 static bool samr_shutdown_cb(void *ptr)
691 const char *rpcsrv_type;
693 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
694 "rpc_server",
695 "epmapper",
696 "none");
698 if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
699 StrCaseCmp(rpcsrv_type, "daemon") == 0) {
700 NTSTATUS status;
702 status = _rpc_ep_unregister(&ndr_table_samr);
703 if (!NT_STATUS_IS_OK(status)) {
704 return false;
708 return true;
711 static bool netlogon_init_cb(void *ptr)
713 struct dcesrv_ep_context *ep_ctx =
714 talloc_get_type_abort(ptr, struct dcesrv_ep_context);
715 struct ndr_syntax_id abstract_syntax = ndr_table_netlogon.syntax_id;
716 const char *pipe_name = "netlogon";
717 const char *rpcsrv_type;
718 uint16_t port;
720 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
721 "rpc_server",
722 "epmapper",
723 "none");
725 if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
726 StrCaseCmp(rpcsrv_type, "daemon") == 0) {
727 NTSTATUS status;
728 bool ok;
730 ok = setup_dcerpc_ncalrpc_socket(ep_ctx->ev_ctx,
731 ep_ctx->msg_ctx,
732 abstract_syntax,
733 pipe_name);
734 if (!ok) {
735 return false;
738 port = _open_sockets(ep_ctx->ev_ctx,
739 ep_ctx->msg_ctx,
740 abstract_syntax,
742 if (port == 0) {
743 return false;
746 status = rpc_ep_setup_register(ep_ctx->ev_ctx,
747 ep_ctx->msg_ctx,
748 &ndr_table_netlogon,
749 pipe_name,
750 port);
751 if (!NT_STATUS_IS_OK(status)) {
752 return false;
756 return true;
759 static bool netlogon_shutdown_cb(void *ptr)
761 const char *rpcsrv_type;
763 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
764 "rpc_server",
765 "epmapper",
766 "none");
768 if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
769 StrCaseCmp(rpcsrv_type, "daemon") == 0) {
770 NTSTATUS status;
772 status = _rpc_ep_unregister(&ndr_table_netlogon);
773 if (!NT_STATUS_IS_OK(status)) {
774 return false;
778 return true;
781 static bool spoolss_init_cb(void *ptr)
783 struct dcesrv_ep_context *ep_ctx =
784 talloc_get_type_abort(ptr, struct dcesrv_ep_context);
785 const char *rpcsrv_type;
786 bool ok;
788 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
789 "rpc_server",
790 "epmapper",
791 "none");
794 * Migrate the printers first.
796 ok = nt_printing_tdb_migrate(ep_ctx->msg_ctx);
797 if (!ok) {
798 return false;
801 if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
802 StrCaseCmp(rpcsrv_type, "daemon") == 0) {
803 NTSTATUS status;
805 status =rpc_ep_setup_register(ep_ctx->ev_ctx,
806 ep_ctx->msg_ctx,
807 &ndr_table_spoolss,
808 "spoolss",
810 if (!NT_STATUS_IS_OK(status)) {
811 return false;
815 return true;
818 static bool spoolss_shutdown_cb(void *ptr)
820 const char *rpcsrv_type;
822 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
823 "rpc_server",
824 "epmapper",
825 "none");
827 srv_spoolss_cleanup();
829 if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
830 StrCaseCmp(rpcsrv_type, "daemon") == 0) {
831 NTSTATUS status;
833 status = _rpc_ep_unregister(&ndr_table_spoolss);
834 if (!NT_STATUS_IS_OK(status)) {
835 return false;
839 return true;
842 static bool svcctl_init_cb(void *ptr)
844 struct dcesrv_ep_context *ep_ctx =
845 talloc_get_type_abort(ptr, struct dcesrv_ep_context);
846 const char *rpcsrv_type;
847 bool ok;
849 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
850 "rpc_server",
851 "epmapper",
852 "none");
854 ok = svcctl_init_winreg(ep_ctx->msg_ctx);
855 if (!ok) {
856 return false;
859 /* initialize the control hooks */
860 init_service_op_table();
862 if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
863 StrCaseCmp(rpcsrv_type, "daemon") == 0) {
864 NTSTATUS status;
866 status = rpc_ep_setup_register(ep_ctx->ev_ctx,
867 ep_ctx->msg_ctx,
868 &ndr_table_svcctl,
869 "svcctl",
871 if (!NT_STATUS_IS_OK(status)) {
872 return false;
876 return true;
879 static bool svcctl_shutdown_cb(void *ptr)
881 const char *rpcsrv_type;
883 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
884 "rpc_server",
885 "epmapper",
886 "none");
887 shutdown_service_op_table();
889 if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
890 StrCaseCmp(rpcsrv_type, "daemon") == 0) {
891 NTSTATUS status;
893 status = _rpc_ep_unregister(&ndr_table_svcctl);
894 if (!NT_STATUS_IS_OK(status)) {
895 return false;
899 return true;
902 static bool ntsvcs_init_cb(void *ptr)
904 struct dcesrv_ep_context *ep_ctx =
905 talloc_get_type_abort(ptr, struct dcesrv_ep_context);
906 const char *rpcsrv_type;
908 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
909 "rpc_server",
910 "epmapper",
911 "none");
913 if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
914 StrCaseCmp(rpcsrv_type, "daemon") == 0) {
915 NTSTATUS status;
917 status = rpc_ep_setup_register(ep_ctx->ev_ctx,
918 ep_ctx->msg_ctx,
919 &ndr_table_ntsvcs,
920 "ntsvcs",
922 if (!NT_STATUS_IS_OK(status)) {
923 return false;
927 return true;
930 static bool ntsvcs_shutdown_cb(void *ptr)
932 const char *rpcsrv_type;
934 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
935 "rpc_server",
936 "epmapper",
937 "none");
939 if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
940 StrCaseCmp(rpcsrv_type, "daemon") == 0) {
941 NTSTATUS status;
943 status = _rpc_ep_unregister(&ndr_table_ntsvcs);
944 if (!NT_STATUS_IS_OK(status)) {
945 return false;
949 return true;
952 static bool eventlog_init_cb(void *ptr)
954 struct dcesrv_ep_context *ep_ctx =
955 talloc_get_type_abort(ptr, struct dcesrv_ep_context);
956 const char *rpcsrv_type;
957 bool ok;
959 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
960 "rpc_server",
961 "epmapper",
962 "none");
964 ok = eventlog_init_winreg(ep_ctx->msg_ctx);
965 if (!ok) {
966 return false;
969 if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
970 StrCaseCmp(rpcsrv_type, "daemon") == 0) {
971 NTSTATUS status;
973 status =rpc_ep_setup_register(ep_ctx->ev_ctx,
974 ep_ctx->msg_ctx,
975 &ndr_table_eventlog,
976 "eventlog",
978 if (!NT_STATUS_IS_OK(status)) {
979 return false;
983 return true;
986 static bool eventlog_shutdown_cb(void *ptr)
988 const char *rpcsrv_type;
990 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
991 "rpc_server",
992 "epmapper",
993 "none");
995 if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
996 StrCaseCmp(rpcsrv_type, "daemon") == 0) {
997 NTSTATUS status;
999 status = _rpc_ep_unregister(&ndr_table_eventlog);
1000 if (!NT_STATUS_IS_OK(status)) {
1001 return false;
1005 return true;
1008 static bool initshutdown_init_cb(void *ptr)
1010 struct dcesrv_ep_context *ep_ctx =
1011 talloc_get_type_abort(ptr, struct dcesrv_ep_context);
1012 const char *rpcsrv_type;
1014 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
1015 "rpc_server",
1016 "epmapper",
1017 "none");
1019 if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
1020 StrCaseCmp(rpcsrv_type, "daemon") == 0) {
1021 NTSTATUS status;
1023 status = rpc_ep_setup_register(ep_ctx->ev_ctx,
1024 ep_ctx->msg_ctx,
1025 &ndr_table_initshutdown,
1026 "initshutdown",
1028 if (!NT_STATUS_IS_OK(status)) {
1029 return false;
1033 return true;
1036 static bool initshutdown_shutdown_cb(void *ptr)
1038 const char *rpcsrv_type;
1040 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
1041 "rpc_server",
1042 "epmapper",
1043 "none");
1045 if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
1046 StrCaseCmp(rpcsrv_type, "daemon") == 0) {
1047 NTSTATUS status;
1049 status = _rpc_ep_unregister(&ndr_table_initshutdown);
1050 if (!NT_STATUS_IS_OK(status)) {
1051 return false;
1055 return true;
1058 #ifdef DEVELOPER
1059 static bool rpcecho_init_cb(void *ptr) {
1060 struct dcesrv_ep_context *ep_ctx =
1061 talloc_get_type_abort(ptr, struct dcesrv_ep_context);
1062 const char *rpcsrv_type;
1063 uint16_t port;
1065 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
1066 "rpc_server",
1067 "epmapper",
1068 "none");
1070 if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
1071 StrCaseCmp(rpcsrv_type, "daemon") == 0) {
1072 NTSTATUS status;
1074 port = _open_sockets(ep_ctx->ev_ctx,
1075 ep_ctx->msg_ctx,
1076 ndr_table_rpcecho.syntax_id,
1078 if (port == 0) {
1079 return false;
1082 status = rpc_ep_setup_register(ep_ctx->ev_ctx,
1083 ep_ctx->msg_ctx,
1084 &ndr_table_rpcecho,
1085 "rpcecho",
1086 port);
1087 if (!NT_STATUS_IS_OK(status)) {
1088 return false;
1092 return true;
1095 static bool rpcecho_shutdown_cb(void *ptr)
1097 const char *rpcsrv_type;
1099 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
1100 "rpc_server",
1101 "epmapper",
1102 "none");
1104 if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
1105 StrCaseCmp(rpcsrv_type, "daemon") == 0) {
1106 NTSTATUS status;
1108 status = _rpc_ep_unregister(&ndr_table_rpcecho);
1109 if (!NT_STATUS_IS_OK(status)) {
1110 return false;
1114 return true;
1116 #endif
1118 static bool netdfs_init_cb(void *ptr)
1120 struct dcesrv_ep_context *ep_ctx =
1121 talloc_get_type_abort(ptr, struct dcesrv_ep_context);
1122 struct ndr_syntax_id abstract_syntax = ndr_table_netdfs.syntax_id;
1123 const char *pipe_name = "netdfs";
1124 const char *rpcsrv_type;
1125 uint16_t port;
1127 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
1128 "rpc_server",
1129 "epmapper",
1130 "none");
1131 if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
1132 StrCaseCmp(rpcsrv_type, "daemon") == 0) {
1133 NTSTATUS status;
1134 bool ok;
1136 ok = setup_dcerpc_ncalrpc_socket(ep_ctx->ev_ctx,
1137 ep_ctx->msg_ctx,
1138 abstract_syntax,
1139 pipe_name);
1140 if (!ok) {
1141 return false;
1144 port = _open_sockets(ep_ctx->ev_ctx,
1145 ep_ctx->msg_ctx,
1146 abstract_syntax,
1148 if (port == 0) {
1149 return false;
1152 status = rpc_ep_setup_register(ep_ctx->ev_ctx,
1153 ep_ctx->msg_ctx,
1154 &ndr_table_netdfs,
1155 pipe_name,
1156 port);
1157 if (!NT_STATUS_IS_OK(status)) {
1158 return false;
1162 return true;
1165 static bool netdfs_shutdown_cb(void *ptr) {
1166 const char *rpcsrv_type;
1168 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
1169 "rpc_server",
1170 "epmapper",
1171 "none");
1173 if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
1174 StrCaseCmp(rpcsrv_type, "daemon") == 0) {
1175 NTSTATUS status;
1177 status = _rpc_ep_unregister(&ndr_table_netdfs);
1178 if (!NT_STATUS_IS_OK(status)) {
1179 return false;
1183 return true;
1186 static bool dssetup_init_cb(void *ptr)
1188 struct dcesrv_ep_context *ep_ctx =
1189 talloc_get_type_abort(ptr, struct dcesrv_ep_context);
1190 struct ndr_syntax_id abstract_syntax = ndr_table_dssetup.syntax_id;
1191 const char *pipe_name = "dssetup";
1192 const char *rpcsrv_type;
1193 uint16_t port;
1195 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
1196 "rpc_server",
1197 "epmapper",
1198 "none");
1200 if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
1201 StrCaseCmp(rpcsrv_type, "daemon") == 0) {
1202 NTSTATUS status;
1203 bool ok;
1205 ok = setup_dcerpc_ncalrpc_socket(ep_ctx->ev_ctx,
1206 ep_ctx->msg_ctx,
1207 abstract_syntax,
1208 pipe_name);
1209 if (!ok) {
1210 return false;
1213 port = _open_sockets(ep_ctx->ev_ctx,
1214 ep_ctx->msg_ctx,
1215 abstract_syntax,
1217 if (port == 0) {
1218 return false;
1221 status = rpc_ep_setup_register(ep_ctx->ev_ctx,
1222 ep_ctx->msg_ctx,
1223 &ndr_table_dssetup,
1224 "dssetup",
1225 port);
1226 if (!NT_STATUS_IS_OK(status)) {
1227 return false;
1231 return true;
1234 static bool dssetup_shutdown_cb(void *ptr) {
1235 const char *rpcsrv_type;
1237 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
1238 "rpc_server",
1239 "epmapper",
1240 "none");
1242 if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
1243 StrCaseCmp(rpcsrv_type, "daemon") == 0) {
1244 NTSTATUS status;
1246 status = _rpc_ep_unregister(&ndr_table_dssetup);
1247 if (!NT_STATUS_IS_OK(status)) {
1248 return false;
1252 return true;
1255 static bool wkssvc_init_cb(void *ptr)
1257 struct dcesrv_ep_context *ep_ctx =
1258 talloc_get_type_abort(ptr, struct dcesrv_ep_context);
1259 struct ndr_syntax_id abstract_syntax = ndr_table_wkssvc.syntax_id;
1260 const char *pipe_name = "wkssvc";
1261 const char *rpcsrv_type;
1262 uint16_t port;
1264 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
1265 "rpc_server",
1266 "epmapper",
1267 "none");
1268 if (StrCaseCmp(rpcsrv_type, "embedded") == 0 ||
1269 StrCaseCmp(rpcsrv_type, "daemon") == 0) {
1270 NTSTATUS status;
1271 bool ok;
1273 ok = setup_dcerpc_ncalrpc_socket(ep_ctx->ev_ctx,
1274 ep_ctx->msg_ctx,
1275 abstract_syntax,
1276 pipe_name);
1277 if (!ok) {
1278 return false;
1281 port = _open_sockets(ep_ctx->ev_ctx,
1282 ep_ctx->msg_ctx,
1283 abstract_syntax,
1285 if (port == 0) {
1286 return false;
1289 status = rpc_ep_setup_register(ep_ctx->ev_ctx,
1290 ep_ctx->msg_ctx,
1291 &ndr_table_wkssvc,
1292 "wkssvc",
1293 port);
1294 if (!NT_STATUS_IS_OK(status)) {
1295 return false;
1299 return true;
1302 static bool wkssvc_shutdown_cb(void *ptr) {
1303 return NT_STATUS_IS_OK(_rpc_ep_unregister(&ndr_table_wkssvc));
1306 bool dcesrv_ep_setup(struct tevent_context *ev_ctx,
1307 struct messaging_context *msg_ctx)
1309 struct dcesrv_ep_context *ep_ctx;
1311 struct rpc_srv_callbacks epmapper_cb;
1313 struct rpc_srv_callbacks winreg_cb;
1314 struct rpc_srv_callbacks srvsvc_cb;
1316 struct rpc_srv_callbacks lsarpc_cb;
1317 struct rpc_srv_callbacks samr_cb;
1318 struct rpc_srv_callbacks netlogon_cb;
1320 struct rpc_srv_callbacks spoolss_cb;
1321 struct rpc_srv_callbacks svcctl_cb;
1322 struct rpc_srv_callbacks ntsvcs_cb;
1323 struct rpc_srv_callbacks eventlog_cb;
1324 struct rpc_srv_callbacks initshutdown_cb;
1325 struct rpc_srv_callbacks netdfs_cb;
1326 #ifdef DEVELOPER
1327 struct rpc_srv_callbacks rpcecho_cb;
1328 #endif
1329 struct rpc_srv_callbacks dssetup_cb;
1330 struct rpc_srv_callbacks wkssvc_cb;
1332 const char *rpcsrv_type;
1334 ep_ctx = talloc(ev_ctx, struct dcesrv_ep_context);
1335 if (ep_ctx == NULL) {
1336 return false;
1339 ep_ctx->ev_ctx = ev_ctx;
1340 ep_ctx->msg_ctx = msg_ctx;
1342 /* start endpoint mapper only if enabled */
1343 rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
1344 "rpc_server",
1345 "epmapper",
1346 "none");
1347 if (StrCaseCmp(rpcsrv_type, "embedded") == 0) {
1348 epmapper_cb.init = epmapper_init_cb;
1349 epmapper_cb.shutdown = epmapper_shutdown_cb;
1350 epmapper_cb.private_data = ep_ctx;
1352 if (!NT_STATUS_IS_OK(rpc_epmapper_init(&epmapper_cb))) {
1353 return false;
1357 winreg_cb.init = winreg_init_cb;
1358 winreg_cb.shutdown = winreg_shutdown_cb;
1359 winreg_cb.private_data = ep_ctx;
1360 if (!NT_STATUS_IS_OK(rpc_winreg_init(&winreg_cb))) {
1361 return false;
1364 srvsvc_cb.init = srvsvc_init_cb;
1365 srvsvc_cb.shutdown = srvsvc_shutdown_cb;
1366 srvsvc_cb.private_data = ep_ctx;
1367 if (!NT_STATUS_IS_OK(rpc_srvsvc_init(&srvsvc_cb))) {
1368 return false;
1372 lsarpc_cb.init = lsarpc_init_cb;
1373 lsarpc_cb.shutdown = lsarpc_shutdown_cb;
1374 lsarpc_cb.private_data = ep_ctx;
1375 if (!NT_STATUS_IS_OK(rpc_lsarpc_init(&lsarpc_cb))) {
1376 return false;
1379 samr_cb.init = samr_init_cb;
1380 samr_cb.shutdown = samr_shutdown_cb;
1381 samr_cb.private_data = ep_ctx;
1382 if (!NT_STATUS_IS_OK(rpc_samr_init(&samr_cb))) {
1383 return false;
1386 netlogon_cb.init = netlogon_init_cb;
1387 netlogon_cb.shutdown = netlogon_shutdown_cb;
1388 netlogon_cb.private_data = ep_ctx;
1389 if (!NT_STATUS_IS_OK(rpc_netlogon_init(&netlogon_cb))) {
1390 return false;
1393 spoolss_cb.init = spoolss_init_cb;
1394 spoolss_cb.shutdown = spoolss_shutdown_cb;
1395 spoolss_cb.private_data = ep_ctx;
1396 if (!NT_STATUS_IS_OK(rpc_spoolss_init(&spoolss_cb))) {
1397 return false;
1401 svcctl_cb.init = svcctl_init_cb;
1402 svcctl_cb.shutdown = svcctl_shutdown_cb;
1403 svcctl_cb.private_data = ep_ctx;
1404 if (!NT_STATUS_IS_OK(rpc_svcctl_init(&svcctl_cb))) {
1405 return false;
1408 ntsvcs_cb.init = ntsvcs_init_cb;
1409 ntsvcs_cb.shutdown = ntsvcs_shutdown_cb;
1410 ntsvcs_cb.private_data = ep_ctx;
1411 if (!NT_STATUS_IS_OK(rpc_ntsvcs_init(&ntsvcs_cb))) {
1412 return false;
1415 eventlog_cb.init = eventlog_init_cb;
1416 eventlog_cb.shutdown = eventlog_shutdown_cb;
1417 eventlog_cb.private_data = ep_ctx;
1418 if (!NT_STATUS_IS_OK(rpc_eventlog_init(&eventlog_cb))) {
1419 return false;
1422 initshutdown_cb.init = initshutdown_init_cb;
1423 initshutdown_cb.shutdown = initshutdown_shutdown_cb;
1424 initshutdown_cb.private_data = ep_ctx;
1425 if (!NT_STATUS_IS_OK(rpc_initshutdown_init(&initshutdown_cb))) {
1426 return false;
1429 netdfs_cb.init = netdfs_init_cb;
1430 netdfs_cb.shutdown = netdfs_shutdown_cb;
1431 netdfs_cb.private_data = ep_ctx;
1432 if (!NT_STATUS_IS_OK(rpc_netdfs_init(&netdfs_cb))) {
1433 return false;
1435 #ifdef DEVELOPER
1437 rpcecho_cb.init = rpcecho_init_cb;
1438 rpcecho_cb.shutdown = rpcecho_shutdown_cb;
1439 rpcecho_cb.private_data = ep_ctx;
1440 if (!NT_STATUS_IS_OK(rpc_rpcecho_init(&rpcecho_cb))) {
1441 return false;
1443 #endif
1445 dssetup_cb.init = dssetup_init_cb;
1446 dssetup_cb.shutdown = dssetup_shutdown_cb;
1447 dssetup_cb.private_data = ep_ctx;
1448 if (!NT_STATUS_IS_OK(rpc_dssetup_init(&dssetup_cb))) {
1449 return false;
1452 wkssvc_cb.init = wkssvc_init_cb;
1453 wkssvc_cb.shutdown = wkssvc_shutdown_cb;
1454 wkssvc_cb.private_data = ep_ctx;
1455 if (!NT_STATUS_IS_OK(rpc_wkssvc_init(&wkssvc_cb))) {
1456 return false;
1459 return true;
1462 /* vim: set ts=8 sw=8 noet cindent ft=c.doxygen: */