s3-rpc_server: Remove remaining code for embedded endpoint mapper
[kai/samba-autobuild/.git] / source3 / rpc_server / rpc_service_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 #include "ntdomain.h"
24
25 #include "../librpc/gen_ndr/ndr_epmapper_c.h"
26 #include "../librpc/gen_ndr/srv_epmapper.h"
27 #include "../librpc/gen_ndr/srv_srvsvc.h"
28 #include "../librpc/gen_ndr/srv_winreg.h"
29 #include "../librpc/gen_ndr/srv_dfs.h"
30 #include "../librpc/gen_ndr/srv_dssetup.h"
31 #include "../librpc/gen_ndr/srv_echo.h"
32 #include "../librpc/gen_ndr/srv_eventlog.h"
33 #include "../librpc/gen_ndr/srv_initshutdown.h"
34 #include "../librpc/gen_ndr/srv_lsa.h"
35 #include "../librpc/gen_ndr/srv_netlogon.h"
36 #include "../librpc/gen_ndr/srv_ntsvcs.h"
37 #include "../librpc/gen_ndr/srv_samr.h"
38 #include "../librpc/gen_ndr/srv_spoolss.h"
39 #include "../librpc/gen_ndr/srv_svcctl.h"
40 #include "../librpc/gen_ndr/srv_wkssvc.h"
41
42 #include "printing/nt_printing_migrate_internal.h"
43 #include "rpc_server/eventlog/srv_eventlog_reg.h"
44 #include "rpc_server/svcctl/srv_svcctl_reg.h"
45 #include "rpc_server/spoolss/srv_spoolss_nt.h"
46 #include "rpc_server/svcctl/srv_svcctl_nt.h"
47
48 #include "librpc/rpc/dcerpc_ep.h"
49 #include "rpc_server/rpc_sock_helper.h"
50 #include "rpc_server/rpc_service_setup.h"
51 #include "rpc_server/rpc_ep_register.h"
52 #include "rpc_server/rpc_server.h"
53 #include "rpc_server/rpc_config.h"
54 #include "rpc_server/epmapper/srv_epmapper.h"
55
56 /* Common routine for embedded RPC servers */
57 static bool rpc_setup_embedded(struct tevent_context *ev_ctx,
58                                struct messaging_context *msg_ctx,
59                                const struct dcerpc_binding_vector *v,
60                                const struct ndr_interface_table *t,
61                                const char *pipe_name)
62 {
63         struct dcerpc_binding_vector *v2;
64         enum rpc_service_mode_e epm_mode = rpc_epmapper_mode();
65         NTSTATUS status;
66         bool ok;
67
68         if (epm_mode != RPC_SERVICE_MODE_DISABLED) {
69                 if (v) {
70                         v2 = dcerpc_binding_vector_dup(talloc_tos(), v);
71                         if (v2 == NULL) {
72                                 return false;
73                         }
74                         status = dcerpc_binding_vector_replace_iface(t, v2);
75                         if (!NT_STATUS_IS_OK(status)) {
76                                 return false;
77                         }
78
79                 } else {
80                         status = dcerpc_binding_vector_new(talloc_tos(), &v2);
81                         if (!NT_STATUS_IS_OK(status)) {
82                                 return false;
83                         }
84                 }
85
86                 status = dcerpc_binding_vector_add_np_default(t, v2);
87                 if (!NT_STATUS_IS_OK(status)) {
88                         return false;
89                 }
90
91                 if (pipe_name) {
92                         ok = setup_dcerpc_ncalrpc_socket(ev_ctx,
93                                                          msg_ctx,
94                                                          pipe_name,
95                                                          NULL);
96                         if (!ok) {
97                                 return false;
98                         }
99
100                         status = dcerpc_binding_vector_add_unix(t, v2, pipe_name);
101                         if (!NT_STATUS_IS_OK(status)) {
102                                 return false;
103                         }
104                 }
105
106                 status = rpc_ep_register(ev_ctx,
107                                          msg_ctx,
108                                          t,
109                                          v2);
110                 if (!NT_STATUS_IS_OK(status)) {
111                         return false;
112                 }
113         }
114
115         return true;
116 }
117
118 static bool rpc_setup_winreg(struct tevent_context *ev_ctx,
119                              struct messaging_context *msg_ctx,
120                              const struct dcerpc_binding_vector *v)
121 {
122         const struct ndr_interface_table *t = &ndr_table_winreg;
123         const char *pipe_name = "winreg";
124         NTSTATUS status;
125         enum rpc_service_mode_e service_mode = rpc_service_mode(t->name);
126         if (service_mode != RPC_SERVICE_MODE_EMBEDDED) {
127                 return true;
128         }
129
130         status = rpc_winreg_init(NULL);
131         if (!NT_STATUS_IS_OK(status)) {
132                 return false;
133         }
134
135         return rpc_setup_embedded(ev_ctx, msg_ctx, v, t, pipe_name);
136 }
137
138 static bool rpc_setup_srvsvc(struct tevent_context *ev_ctx,
139                              struct messaging_context *msg_ctx,
140                              const struct dcerpc_binding_vector *v)
141 {
142         const struct ndr_interface_table *t = &ndr_table_srvsvc;
143         const char *pipe_name = "srvsvc";
144         NTSTATUS status;
145         enum rpc_service_mode_e service_mode = rpc_service_mode(t->name);
146         if (service_mode != RPC_SERVICE_MODE_EMBEDDED) {
147                 return true;
148         }
149
150         status = rpc_srvsvc_init(NULL);
151         if (!NT_STATUS_IS_OK(status)) {
152                 return false;
153         }
154
155         return rpc_setup_embedded(ev_ctx, msg_ctx, v, t, pipe_name);
156 }
157
158 static bool rpc_setup_lsarpc(struct tevent_context *ev_ctx,
159                              struct messaging_context *msg_ctx,
160                              const struct dcerpc_binding_vector *v)
161 {
162         const struct ndr_interface_table *t = &ndr_table_lsarpc;
163         const char *pipe_name = "lsarpc";
164         enum rpc_daemon_type_e lsasd_type = rpc_lsasd_daemon();
165         NTSTATUS status;
166         enum rpc_service_mode_e service_mode = rpc_service_mode(t->name);
167         if (service_mode != RPC_SERVICE_MODE_EMBEDDED || lsasd_type != RPC_DAEMON_EMBEDDED) {
168                 return true;
169         }
170
171         status = rpc_lsarpc_init(NULL);
172         if (!NT_STATUS_IS_OK(status)) {
173                 return false;
174         }
175
176         return rpc_setup_embedded(ev_ctx, msg_ctx, v, t, pipe_name);
177 }
178
179 static bool rpc_setup_samr(struct tevent_context *ev_ctx,
180                            struct messaging_context *msg_ctx,
181                            const struct dcerpc_binding_vector *v)
182 {
183         const struct ndr_interface_table *t = &ndr_table_samr;
184         const char *pipe_name = "samr";
185         enum rpc_daemon_type_e lsasd_type = rpc_lsasd_daemon();
186         NTSTATUS status;
187         enum rpc_service_mode_e service_mode = rpc_service_mode(t->name);
188         if (service_mode != RPC_SERVICE_MODE_EMBEDDED || lsasd_type != RPC_DAEMON_EMBEDDED) {
189                 return true;
190         }
191
192         status = rpc_samr_init(NULL);
193         if (!NT_STATUS_IS_OK(status)) {
194                 return false;
195         }
196
197         return rpc_setup_embedded(ev_ctx, msg_ctx, v, t, pipe_name);
198 }
199
200 static bool rpc_setup_netlogon(struct tevent_context *ev_ctx,
201                                struct messaging_context *msg_ctx,
202                                const struct dcerpc_binding_vector *v)
203 {
204         const struct ndr_interface_table *t = &ndr_table_netlogon;
205         const char *pipe_name = "netlogon";
206         enum rpc_daemon_type_e lsasd_type = rpc_lsasd_daemon();
207         NTSTATUS status;
208         enum rpc_service_mode_e service_mode = rpc_service_mode(t->name);
209         if (service_mode != RPC_SERVICE_MODE_EMBEDDED || lsasd_type != RPC_DAEMON_EMBEDDED) {
210                 return true;
211         }
212
213         status = rpc_netlogon_init(NULL);
214         if (!NT_STATUS_IS_OK(status)) {
215                 return false;
216         }
217
218         return rpc_setup_embedded(ev_ctx, msg_ctx, v, t, pipe_name);
219 }
220
221 static bool rpc_setup_netdfs(struct tevent_context *ev_ctx,
222                              struct messaging_context *msg_ctx,
223                              const struct dcerpc_binding_vector *v)
224 {
225         const struct ndr_interface_table *t = &ndr_table_netdfs;
226         const char *pipe_name = "netdfs";
227         NTSTATUS status;
228         enum rpc_service_mode_e service_mode = rpc_service_mode(t->name);
229         if (service_mode != RPC_SERVICE_MODE_EMBEDDED) {
230                 return true;
231         }
232
233         status = rpc_netdfs_init(NULL);
234         if (!NT_STATUS_IS_OK(status)) {
235                 return false;
236         }
237
238         return rpc_setup_embedded(ev_ctx, msg_ctx, v, t, pipe_name);
239 }
240
241 #ifdef DEVELOPER
242 static bool rpc_setup_rpcecho(struct tevent_context *ev_ctx,
243                               struct messaging_context *msg_ctx,
244                               const struct dcerpc_binding_vector *v)
245 {
246         const struct ndr_interface_table *t = &ndr_table_rpcecho;
247         const char *pipe_name = "rpcecho";
248         NTSTATUS status;
249         enum rpc_service_mode_e service_mode = rpc_service_mode(t->name);
250         if (service_mode != RPC_SERVICE_MODE_EMBEDDED) {
251                 return true;
252         }
253
254         status = rpc_rpcecho_init(NULL);
255         if (!NT_STATUS_IS_OK(status)) {
256                 return false;
257         }
258
259         return rpc_setup_embedded(ev_ctx, msg_ctx, v, t, pipe_name);
260 }
261 #endif
262
263 static bool rpc_setup_dssetup(struct tevent_context *ev_ctx,
264                               struct messaging_context *msg_ctx,
265                               const struct dcerpc_binding_vector *v)
266 {
267         const struct ndr_interface_table *t = &ndr_table_dssetup;
268         const char *pipe_name = "dssetup";
269         NTSTATUS status;
270         enum rpc_service_mode_e service_mode = rpc_service_mode(t->name);
271         if (service_mode != RPC_SERVICE_MODE_EMBEDDED) {
272                 return true;
273         }
274
275         status = rpc_dssetup_init(NULL);
276         if (!NT_STATUS_IS_OK(status)) {
277                 return false;
278         }
279
280         return rpc_setup_embedded(ev_ctx, msg_ctx, v, t, pipe_name);
281 }
282
283 static bool rpc_setup_wkssvc(struct tevent_context *ev_ctx,
284                              struct messaging_context *msg_ctx,
285                              const struct dcerpc_binding_vector *v)
286 {
287         const struct ndr_interface_table *t = &ndr_table_wkssvc;
288         const char *pipe_name = "wkssvc";
289         NTSTATUS status;
290         enum rpc_service_mode_e service_mode = rpc_service_mode(t->name);
291         if (service_mode != RPC_SERVICE_MODE_EMBEDDED) {
292                 return true;
293         }
294
295         status = rpc_wkssvc_init(NULL);
296         if (!NT_STATUS_IS_OK(status)) {
297                 return false;
298         }
299
300         return rpc_setup_embedded(ev_ctx, msg_ctx, v, t, pipe_name);
301 }
302
303 static bool spoolss_init_cb(void *ptr)
304 {
305         struct messaging_context *msg_ctx =
306                 talloc_get_type_abort(ptr, struct messaging_context);
307         bool ok;
308
309         /*
310          * Migrate the printers first.
311          */
312         ok = nt_printing_tdb_migrate(msg_ctx);
313         if (!ok) {
314                 return false;
315         }
316
317         return true;
318 }
319
320 static bool spoolss_shutdown_cb(void *ptr)
321 {
322         srv_spoolss_cleanup();
323
324         return true;
325 }
326
327 static bool rpc_setup_spoolss(struct tevent_context *ev_ctx,
328                               struct messaging_context *msg_ctx)
329 {
330         const struct ndr_interface_table *t = &ndr_table_spoolss;
331         struct rpc_srv_callbacks spoolss_cb;
332         enum rpc_daemon_type_e spoolss_type = rpc_spoolss_daemon();
333         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
334         enum rpc_service_mode_e service_mode = rpc_service_mode(t->name);
335
336         if (_lp_disable_spoolss()) {
337                 return true;
338         }
339
340         if (service_mode != RPC_SERVICE_MODE_EMBEDDED || spoolss_type != RPC_DAEMON_EMBEDDED) {
341                 return true;
342         }
343
344         spoolss_cb.init         = spoolss_init_cb;
345         spoolss_cb.shutdown     = spoolss_shutdown_cb;
346         spoolss_cb.private_data = msg_ctx;
347
348         status = rpc_spoolss_init(&spoolss_cb);
349         if (!NT_STATUS_IS_OK(status)) {
350                 return false;
351         }
352
353         return rpc_setup_embedded(ev_ctx, msg_ctx, NULL, t, NULL);
354 }
355
356 static bool svcctl_init_cb(void *ptr)
357 {
358         struct messaging_context *msg_ctx =
359                 talloc_get_type_abort(ptr, struct messaging_context);
360         bool ok;
361
362         /* initialize the control hooks */
363         init_service_op_table();
364
365         ok = svcctl_init_winreg(msg_ctx);
366         if (!ok) {
367                 return false;
368         }
369
370         return true;
371 }
372
373 static bool svcctl_shutdown_cb(void *ptr)
374 {
375         shutdown_service_op_table();
376
377         return true;
378 }
379
380 static bool rpc_setup_svcctl(struct tevent_context *ev_ctx,
381                              struct messaging_context *msg_ctx)
382 {
383         const struct ndr_interface_table *t = &ndr_table_svcctl;
384         const char *pipe_name = "svcctl";
385         struct rpc_srv_callbacks svcctl_cb;
386         NTSTATUS status;
387         enum rpc_service_mode_e service_mode = rpc_service_mode(t->name);
388         if (service_mode != RPC_SERVICE_MODE_EMBEDDED) {
389                 return true;
390         }
391
392         svcctl_cb.init         = svcctl_init_cb;
393         svcctl_cb.shutdown     = svcctl_shutdown_cb;
394         svcctl_cb.private_data = msg_ctx;
395
396         status = rpc_svcctl_init(&svcctl_cb);
397         if (!NT_STATUS_IS_OK(status)) {
398                 return false;
399         }
400
401         return rpc_setup_embedded(ev_ctx, msg_ctx, NULL, t, pipe_name);
402 }
403
404 static bool rpc_setup_ntsvcs(struct tevent_context *ev_ctx,
405                              struct messaging_context *msg_ctx)
406 {
407         const struct ndr_interface_table *t = &ndr_table_ntsvcs;
408         NTSTATUS status;
409         enum rpc_service_mode_e service_mode = rpc_service_mode(t->name);
410         if (service_mode != RPC_SERVICE_MODE_EMBEDDED) {
411                 return true;
412         }
413
414         status = rpc_ntsvcs_init(NULL);
415         if (!NT_STATUS_IS_OK(status)) {
416                 return false;
417         }
418
419         return rpc_setup_embedded(ev_ctx, msg_ctx, NULL, t, NULL);
420
421         return true;
422 }
423
424 static bool eventlog_init_cb(void *ptr)
425 {
426         struct messaging_context *msg_ctx =
427                 talloc_get_type_abort(ptr, struct messaging_context);
428         bool ok;
429
430         ok = eventlog_init_winreg(msg_ctx);
431         if (!ok) {
432                 return false;
433         }
434
435         return true;
436 }
437
438 static bool rpc_setup_eventlog(struct tevent_context *ev_ctx,
439                                struct messaging_context *msg_ctx)
440 {
441         const struct ndr_interface_table *t = &ndr_table_eventlog;
442         struct rpc_srv_callbacks eventlog_cb;
443         NTSTATUS status;
444         enum rpc_service_mode_e service_mode = rpc_service_mode(t->name);
445         if (service_mode != RPC_SERVICE_MODE_EMBEDDED) {
446                 return true;
447         }
448
449         eventlog_cb.init         = eventlog_init_cb;
450         eventlog_cb.shutdown     = NULL;
451         eventlog_cb.private_data = msg_ctx;
452
453         status = rpc_eventlog_init(&eventlog_cb);
454         if (!NT_STATUS_IS_OK(status)) {
455                 return false;
456         }
457
458         return rpc_setup_embedded(ev_ctx, msg_ctx, NULL, t, NULL);
459 }
460
461 static bool rpc_setup_initshutdown(struct tevent_context *ev_ctx,
462                                    struct messaging_context *msg_ctx)
463 {
464         const struct ndr_interface_table *t = &ndr_table_initshutdown;
465         NTSTATUS status;
466         enum rpc_service_mode_e service_mode = rpc_service_mode(t->name);
467         if (service_mode != RPC_SERVICE_MODE_EMBEDDED) {
468                 return true;
469         }
470
471         status = rpc_initshutdown_init(NULL);
472         if (!NT_STATUS_IS_OK(status)) {
473                 return false;
474         }
475
476         return rpc_setup_embedded(ev_ctx, msg_ctx, NULL, t, NULL);
477 }
478
479 bool dcesrv_ep_setup(struct tevent_context *ev_ctx,
480                      struct messaging_context *msg_ctx)
481 {
482         enum rpc_service_mode_e epm_mode = rpc_epmapper_mode();
483         struct dcerpc_binding_vector *v;
484         const char *rpcsrv_type;
485         TALLOC_CTX *tmp_ctx;
486         NTSTATUS status;
487         bool ok;
488
489         tmp_ctx = talloc_stackframe();
490         if (tmp_ctx == NULL) {
491                 return false;
492         }
493
494         status = dcerpc_binding_vector_new(tmp_ctx,
495                                            &v);
496         if (!NT_STATUS_IS_OK(status)) {
497                 ok = false;
498                 goto done;
499         }
500
501         rpcsrv_type = lp_parm_const_string(GLOBAL_SECTION_SNUM,
502                                            "rpc_server",
503                                            "tcpip",
504                                            "no");
505
506         if ((strcasecmp_m(rpcsrv_type, "yes") == 0 ||
507              strcasecmp_m(rpcsrv_type, "true") == 0)
508             && epm_mode != RPC_SERVICE_MODE_DISABLED) {
509                 status = rpc_setup_tcpip_sockets(ev_ctx,
510                                                  msg_ctx,
511                                                  &ndr_table_winreg,
512                                                  v,
513                                                  0);
514                 if (!NT_STATUS_IS_OK(status)) {
515                         ok = false;
516                         goto done;
517                 }
518         }
519
520         ok = rpc_setup_winreg(ev_ctx, msg_ctx, v);
521         if (!ok) {
522                 goto done;
523         }
524
525         ok = rpc_setup_srvsvc(ev_ctx, msg_ctx, v);
526         if (!ok) {
527                 goto done;
528         }
529
530         ok = rpc_setup_lsarpc(ev_ctx, msg_ctx, v);
531         if (!ok) {
532                 goto done;
533         }
534
535         ok = rpc_setup_samr(ev_ctx, msg_ctx, v);
536         if (!ok) {
537                 goto done;
538         }
539
540         ok = rpc_setup_netlogon(ev_ctx, msg_ctx, v);
541         if (!ok) {
542                 goto done;
543         }
544
545         ok = rpc_setup_netdfs(ev_ctx, msg_ctx, v);
546         if (!ok) {
547                 goto done;
548         }
549
550 #ifdef DEVELOPER
551         ok = rpc_setup_rpcecho(ev_ctx, msg_ctx, v);
552         if (!ok) {
553                 goto done;
554         }
555 #endif
556
557         ok = rpc_setup_dssetup(ev_ctx, msg_ctx, v);
558         if (!ok) {
559                 goto done;
560         }
561
562         ok = rpc_setup_wkssvc(ev_ctx, msg_ctx, v);
563         if (!ok) {
564                 goto done;
565         }
566
567         ok = rpc_setup_spoolss(ev_ctx, msg_ctx);
568         if (!ok) {
569                 goto done;
570         }
571
572         ok = rpc_setup_svcctl(ev_ctx, msg_ctx);
573         if (!ok) {
574                 goto done;
575         }
576
577         ok = rpc_setup_ntsvcs(ev_ctx, msg_ctx);
578         if (!ok) {
579                 goto done;
580         }
581
582         ok = rpc_setup_eventlog(ev_ctx, msg_ctx);
583         if (!ok) {
584                 goto done;
585         }
586
587         ok = rpc_setup_initshutdown(ev_ctx, msg_ctx);
588         if (!ok) {
589                 goto done;
590         }
591
592 done:
593         talloc_free(tmp_ctx);
594         return ok;
595 }
596
597 /* vim: set ts=8 sw=8 noet cindent ft=c.doxygen: */